#include "JSONObject.h"
#include "JSONArray.h"
#include "JSONString.h"

JSONObject::JSONObject(void)
{
}

JSONObject::~JSONObject(void)
{
	JSONObjecDATA::iterator pItr = m_data.begin();
	for(; pItr != m_data.end(); ++pItr){
		delete pItr->second;
		pItr->second = 0;
	}
}

void JSONObject::setData(std::istream &is)
{
	char c;
	do{
		std::string key = getString(is);

		is.get();

		c = is.get();
		JSONValue *pValue = 0;
		switch(c){
			case '{' : pValue = new JSONObject;
					   pValue->setData(is);
					   break;
			case '[' : pValue = new JSONArray;
					   pValue->setData(is);
					   break;
			default:   is.putback(c);
					   pValue = new JSONString;
					   pValue->setData(is);
		}

		m_data[key] = pValue;

		c = is.get();
	}while(c == ',');
	//is.putback(c);
}

JSONValue * JSONObject::getData(std::string key)
{
	return m_data[key];
}